﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace ControlsLib
{
    public partial class AssemblyTypePicker : Form
    {
        private string m_typeName = string.Empty;
        private Type m_type = null;
        private string m_assemblyPath = string.Empty;
        private Assembly m_assembly = null;

        public Type SelectedType
        {
            get { return m_type; }
            private set { m_type = value; }
        }
        
        public string AssemblyPath
        {
            get { return m_assemblyPath; }
            set 
            { 
                m_assemblyPath = value;

                this.chklbTypes.Items.Clear();

                m_assembly = Assembly.LoadFrom(m_assemblyPath);

                foreach (Type t in m_assembly.GetTypes())
                {
                    this.chklbTypes.Items.Add(t.FullName);
                }
            }
        }

        public AssemblyTypePicker()
        {
            InitializeComponent();

            this.Load += new EventHandler(AssemblyTypePicker_Load);
        }

        void AssemblyTypePicker_Load(object sender, EventArgs e)
        {
            //// throw new NotImplementedException();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            m_typeName = this.chklbTypes.SelectedItem.ToString();

            m_type = m_assembly.GetType(m_typeName);
        }

        private void btnChoosePath_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.textBox1.Text = openFileDialog1.FileName;
                this.AssemblyPath = openFileDialog1.FileName;
            }
        }
    }
}
